package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"rent-ease/models/cache"
	"rent-ease/models/mysql"
	"time"

	"rent-ease/honesrpc/honesrpc"
	"rent-ease/honesrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HonesListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHonesListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HonesListLogic {
	return &HonesListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表
func (l *HonesListLogic) HonesList(in *honesrpc.HonesListRequest) (*honesrpc.HonesListResponse, error) {
	// todo: add your logic here and delete this line
	var list []*honesrpc.HonesList
	key := fmt.Sprintf("list_%s", in)
	result, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if result == "" {
		var err error
		list, err = mysql.HonesList(mysql.DB, list, in)
		if err != nil {
			return nil, err
		}
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, key, marshal, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &honesrpc.HonesListResponse{
		List: list,
	}, nil
}
